package test_tcp_client

import (
	"fmt"
	"log"
	"net"
	"time"
)

func TcpClient(v string) {
	conn, err := net.DialTCP("tcp4", nil, &net.TCPAddr{
		IP:   net.ParseIP("127.0.0.1"),
		Port: 5567,
	})
	if err != nil {
		log.Println(err)
		return
	}

	{
		var puId = "PU_123"
		data := fmt.Sprintf("%v\n", puId)

		_, _ = conn.Write([]byte(data))
	}

	go func() {
		buff := make([]byte, 1024)
		for {
			n, err := conn.Read(buff)
			if err != nil {
				break
			}

			log.Println(v, string(buff[:n]))
		}
	}()

	for i := 0; ; i++ {
		time.Sleep(time.Second)

		_, _ = conn.Write([]byte(fmt.Sprintf("%v hello %v", v, i)))
	}
}
